OPERATORS AND EXPRESSIONS

In Python, operators are symbols that perform various operations on one or more operands (values). An expression is a combination of variables, literals, and operators that can be evaluated to produce a result. Python supports a wide range of operators for different purposes.

Here are some of the commonly used operators in Python:

  1. Arithmetic Operators: These operators are used for basic arithmetic operations.
  • +: Addition
  • -: Subtraction
  • *: Multiplication
  • /: Division (results in a float)
  • //: Floor Division (results in an integer)
  • %: Modulo (remainder of the division)
  • **: Exponentiation (power)

Example:

python
x = 10 y = 3 print(x + y) # Output: 13 print(x - y) # Output: 7 print(x * y) # Output: 30 print(x / y) # Output: 3.3333333333333335 print(x // y) # Output: 3 print(x % y) # Output: 1 print(x ** y) # Output: 1000
  1. Comparison Operators: These operators are used to compare values and return Boolean values (True or False).
  • ==: Equal to
  • !=: Not equal to
  • <: Less than
  • >: Greater than
  • <=: Less than or equal to
  • >=: Greater than or equal to

Example:

python
a = 5 b = 10 print(a == b) # Output: False print(a != b) # Output: True print(a < b) # Output: True print(a > b) # Output: False print(a <= b) # Output: True print(a >= b) # Output: False
  1. Logical Operators: These operators are used to perform logical operations and return Boolean values.
  • and: Logical AND (returns True if both operands are True)
  • or: Logical OR (returns True if at least one operand is True)
  • not: Logical NOT (returns the opposite Boolean value)

Example:

python
p = True q = False print(p and q) # Output: False print(p or q) # Output: True print(not p) # Output: False
  1. Assignment Operators: These operators are used to assign values to variables.
  • =: Assigns the value on the right to the variable on the left.
  • +=: Adds the right operand to the left operand and assigns the result to the left operand.
  • `-=``: Subtracts the right operand from the left operand and assigns the result to the left operand.
  • `*=``: Multiplies the left operand by the right operand and assigns the result to the left operand.
  • `/=``: Divides the left operand by the right operand and assigns the result to the left operand.
  • `//=``: Performs floor division on the left operand by the right operand and assigns the result to the left operand.
  • `%=``: Calculates the remainder of the division of the left operand by the right operand and assigns the result to the left operand.
  • `**=``: Raises the left operand to the power of the right operand and assigns the result to the left operand.

Example:

python
x = 5 x += 3 # Equivalent to x = x + 3 print(x) # Output: 8 y = 10 y **= 2 # Equivalent to y = y ** 2 print(y) # Output: 100
  1. Other Operators: There are several other operators in Python, including bitwise operators, membership operators, and identity operators. However, these are less commonly used in everyday programming tasks.

Understanding operators and expressions is essential for writing effective and meaningful Python programs, as they allow you to manipulate data and perform different actions based on conditions and values.